package com.chwn.curiousdiary.ui.register;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;

import com.chwn.curiousdiary.R;
import com.chwn.curiousdiary.bean.RegisterBean;
import com.chwn.curiousdiary.gloable.Constant;
import com.chwn.curiousdiary.gloable.CustomEvent;
import com.chwn.curiousdiary.gloable.User;
import com.chwn.curiousdiary.utils.CustomToast;
import com.chwn.curiousdiary.utils.DialogUtil;
import com.chwn.curiousdiary.utils.SendCodeUtil;
import com.chwn.harulib.base.BaseActivity;
import com.chwn.harulib.util.RegexValidateUtil;
import com.chwn.harulib.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * Created by Myshikin on 2017/6/17.
 */

public class PhoneActivity extends BaseActivity {

    @BindView(R.id.et_user_phone)
    EditText  mUserPhoneET;
    @BindView(R.id.iv_next_step)
    ImageView mNextStep;

    /**
     *
     */
    private int mPageType;

    private String unionid;
    private String phone;
    private User user;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);  //全屏
        super.onCreate(savedInstanceState);

        EventBus.getDefault().register(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_phone;
    }

    public void onEventMainThread(CustomEvent event) {
        switch (event.getState()) {
            case CustomEvent.ClOSE_SELF:
                finish();
                if (EventBus.getDefault().isRegistered(this)) {
                    EventBus.getDefault().unregister(this);
                }
                break;
        }
    }

    @Override
    protected void initData() {
        super.initData();
        mPageType = getIntent().getIntExtra(Constant.JUMP_TYPE, Constant.FGT_PWD);
        unionid = getIntent().getStringExtra(Constant.UNION_ID);
        user = (User) getIntent().getSerializableExtra(Constant.USER);
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        setBackTVisible(true);
        setBackIcon(R.drawable.icon_back_light);
    }

    @Override
    protected void initListener() {
        super.initListener();
    }

    private boolean isSend;
    RegisterBean info;

    private void checkInput() {
        phone = mUserPhoneET.getText().toString();

        if (TextUtils.isEmpty(phone)) {
            ToastUtil.shortShowToast("请输入手机号");
            return;
        } else if (!RegexValidateUtil.checkCellphone(phone)) {
            ToastUtil.shortShowToast("请输入正确的手机号");
            return;
        }

        if (isSend) {
            nextStep();
        } else {
            sendVerifyCode();
        }
    }

    private void nextStep() {
        Intent intent = new Intent(PhoneActivity.this, VerifyCodeActivity.class);
        intent.putExtra(Constant.JUMP_TYPE, mPageType);
        intent.putExtra(Constant.PHONE, phone);
        intent.putExtra(Constant.UNION_ID, unionid);
        intent.putExtra(Constant.IS_REGISTER, info);
        intent.putExtra(Constant.USER , user);
        startActivity(intent);
    }

    private void sendVerifyCode() {
        SendCodeUtil.sendVerifyCode(phone, mPageType, unionid, new SendCodeUtil.CallBack() {
            @Override
            public void onStart() {
                showWaitDialog("发送中");
            }

            @Override
            public void onSuccess(RegisterBean info) {
                PhoneActivity.this.info = info ;
                DialogUtil.dismissWaitDialog();
                nextStep();
                isSend = true;
            }

            @Override
            public void onFail(String text, int backType) {
                DialogUtil.dismissWaitDialog();
                CustomToast.showShortToast(text);
                if (backType == 2) {
//                    finish();
                }
            }
        });
    }

    @OnClick({R.id.ll_back, R.id.iv_next_step})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.iv_next_step:
                checkInput();
                break;
        }
    }

    void showWaitDialog(String text) {
        DialogUtil.showWaitDialog(PhoneActivity.this, text, false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
